<!--#include file="./header.inc"-->
<!--#include file="../core/md5.inc"-->
<%

select case aq
	'admin
	case "save" 		: 	call save
	case "edit" 		: 	call edit
	case "status" 		: 	call status
	case "delete" 		: 	call delete
	case else 			: 	call index
end select

call db.close()

function index()
    dim ip
    ip = admin.filter(request("ip"))
    sql = "select id,adminName,nickName,loginIP,loginTime,loginNumber,loginInfo,isStop,isSuper from cj_admin"
    if ip <> "" then sql = sql & " where loginInfo like '%" & ip & "%'"
    sql = sql & " Order By id desc"
    temp = db.arrayData(sql, page, rows)
	infoTotal = temp(0)
	data = temp(1)
	%><!--#include file="./view/admin.index.html"--><%
end function

function edit()
	data = array("", "", "", "0", "0")
	if iid > 0 then
		set rs = db.exec("select adminName,nickName,loginInfo,isStop,isSuper from cj_admin where id=" & iid, 1)
		if rs.eof then admin.message "id不存在！"
		data = rs
		set rs = nothing
		if iid <> adminInfo(0) then
			if not(isSuper) and app.cj_bool(data(4)) then
				admin.message "权限不足！"
			end if
		end if
	end if
	%><!--#include file="./view/admin.edit.html"--><%
end function

function save()
	dim adminName, nickName, iPass, loginInfo
	adminName = admin.filter(request.form("adminName"))
	nickName = admin.filter(request.form("nickName"))
	loginInfo = trim(request.form("loginInfo"))
	if len(adminName) = 0 then admin.message "登录名不能为空！"
	if len(nickName) = 0 then admin.message "昵称不能为空！"
	if iid > 0 then
		if admin.queryTable("adminName", "admin where id<>" & iid & " and adminName='" & adminName & "'") <> false then admin.message "已存在同名管理员！"
		set rs = db.exec("select * from cj_admin where id=" & iid, 3)
		if rs.eof then admin.message "id不存在！"
		rs("loginInfo") = loginInfo
	else
		iPass = trim(request.form("password"))
		if iPass = "" then admin.message "密码不能为空！"
		set rs = db.exec("select * from cj_admin", 3)
		rs.AddNew
		rs("password") = getMd5(iPass)
	end if
	if isSuper then
		rs("isStop") = admin.isCheck(request.form("isStop"))
		rs("isSuper") = admin.isCheck(request.form("isSuper"))
	end if
	rs("adminName") = adminName
	rs("nickName") = nickName
	rs.update
	rs.close : set rs = nothing
	if iid > 0 then
		admin.back "修改成功！", "返回列表", "?q=index"
	else
		admin.back "添加成功！", "继续添加", "?q=edit"
	end if
end function

function delete()
	if not(isSuper) then app.json "权限不足！", 1
    if iid = 0 then app.json "请选择ID！", 1
	call db.exec("Delete from cj_admin where id=" & iid, 1)
	app.json "删除成功！", 0
end function

function status()
	dim isStop, msg
	if not(isSuper) then app.json "权限不足！", 1
    if iid = 0 then app.json "请选择ID！", 1
    isStop = app.cj_bool(request("isStop"))
    msg = "启用"
    if isStop then
        msg = "禁用"
    end if
	call db.exec("update cj_admin set isStop=" & isStop & " where id=" & iid, 1)
	app.json msg & "成功！", 0
end function

%>